package com.jcli.novel_t.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;

/**
 * 参照TextFileProcessor01编写的界面，不好看。
 */
public class TextFileProcessorUI04 extends JFrame {
    private JTextArea inputTextArea;
    private JButton processButton;
    private JTextArea outputTextArea;
    private Properties properties;

    public TextFileProcessorUI04(Properties properties) {
        this.properties = properties;
        setTitle("Text File Processor");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create components
        inputTextArea = new JTextArea(20, 40);
        processButton = new JButton("Process File");
        outputTextArea = new JTextArea(20, 40);

        // Create panels
        JPanel inputPanel = new JPanel();
        inputPanel.add(new JScrollPane(inputTextArea));
        inputPanel.add(processButton);

        JPanel outputPanel = new JPanel();
        outputPanel.add(new JScrollPane(outputTextArea));

        // Add panels to the frame
        getContentPane().setLayout(new GridLayout(1, 2));
        getContentPane().add(inputPanel);
        getContentPane().add(outputPanel);

        // Add action listeners
        processButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                processFile();
            }
        });
    }

    private void processFile() {
        String inputText = inputTextArea.getText();
        String processedText = processText(inputText);
        outputTextArea.setText(processedText);
    }

    private String processText(String text) {
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            String escapedKey = Pattern.quote(key);

            // 检查文本是否包含键，如果包含则进行替换
            if (text.contains(key)) {
                text = text.replaceAll(escapedKey, value);
            }
        }
        return text;
    }

    public static void main(String[] args) {
        Properties defaultProperties = new Properties();
        Properties customProperties = new Properties();

        try (InputStream defaultInputStream = new FileInputStream("src/main/resources/config.properties");
             InputStream customInputStream = new FileInputStream("app.properties")) {
            defaultProperties.load(new InputStreamReader(defaultInputStream, "UTF-8"));
            customProperties.load(new InputStreamReader(customInputStream, "UTF-8"));
        } catch (IOException e) {
            System.err.println("无法加载配置文件: " + e.getMessage());
            return;
        }

        // 合并两个配置文件的内容
        Properties mergedProperties = new Properties();
        mergedProperties.putAll(defaultProperties);
        mergedProperties.putAll(customProperties);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TextFileProcessorUI04 ui = new TextFileProcessorUI04(mergedProperties);
                ui.setVisible(true);
            }
        });
    }
}
